package com.softserveinc.interactive_board.web;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.softserveinc.interactive_board.commons.CheckedPresentationsList;
import com.softserveinc.interactive_board.domain.Presentation;
import com.softserveinc.interactive_board.domain.User;
import com.softserveinc.interactive_board.service.PresentationService;
import com.softserveinc.interactive_board.validators.form.EditPresentationForm;
import com.softserveinc.interactive_board.validators.form.EditUserForm;

@Controller
@RequestMapping("/administration/presentations")
public class AdminPresentationsController {

	private static final String INDEX_FILE_LOCATION = "administration/presentations/index";
	private static final String ADD_PRES_FILE_LOCATION = "administration/presentations/add";
	private static final String EDIT_PRES_FILE_LOCATION = "administration/presentations/edit";

	private static final String ADD_PRESFORM_ATTR_NAME = "editPresentationForm";
	private static final String EDIT_PRESFORM_ATTR_NAME = "editPresentationForm";
	private static final String PRESSTATUS_ATTR_NAME = "presStatusesAttr";
	private static final String PRESENTATIONS_ATTR_NAME = "checkedPresentationsList";

	private static final String FIELD_STAUS_ID = "status_id";

	@RequestMapping(method = RequestMethod.GET)
	public String def() {
		return "redirect:/administration/presentations/";
	}

	@RequestMapping("/")
	public String presentations(Model model) {
		model.addAttribute(
				AdminPresentationsController.PRESENTATIONS_ATTR_NAME,
				new CheckedPresentationsList(presentationService
						.getPresentatios()));
		return AdminPresentationsController.INDEX_FILE_LOCATION;
	}

	@RequestMapping("/filter")
	public String presStatFilter(@RequestParam("field") String field,
			@RequestParam("status_id") int statusId,
			@RequestParam("presentor_id") int presentorId, Model model) {
		if (field.equals(AdminPresentationsController.FIELD_STAUS_ID))
			model.addAttribute(
					AdminPresentationsController.PRESENTATIONS_ATTR_NAME,
					new CheckedPresentationsList(presentationService
							.getPresentationsByStatusId(statusId)));
		else
			model.addAttribute(
					AdminPresentationsController.PRESENTATIONS_ATTR_NAME,
					new CheckedPresentationsList(presentationService
							.getPresentationsByPresentorId(presentorId)));
		return AdminPresentationsController.INDEX_FILE_LOCATION;
	}

	// ----------------------------Add-------------------------------------
	@RequestMapping(method = RequestMethod.GET, value = "/add")
	public String presAdd(Model model) {
		model.addAttribute(AdminPresentationsController.ADD_PRESFORM_ATTR_NAME,
				new EditPresentationForm());
		model.addAttribute(AdminPresentationsController.PRESSTATUS_ATTR_NAME,
				this.presentationService.getPresentationStatuses());
		return AdminPresentationsController.ADD_PRES_FILE_LOCATION;
	}

	@RequestMapping(method = RequestMethod.POST, value = "/add")
	public String presAdd(Model model, @Valid final EditPresentationForm form,
			final BindingResult result) {
		if (result.hasErrors()) {
			model.addAttribute(
					AdminPresentationsController.PRESSTATUS_ATTR_NAME,
					this.presentationService.getPresentationStatuses());
			return AdminPresentationsController.ADD_PRES_FILE_LOCATION;
		}
		this.presentationService.addPresentation(form.getPresentation());
		return "redirect:/administration/presentations/";
	}

	// ----------------------------Delete-------------------------------------
	@RequestMapping("/delete")
	public String userDelete(@RequestParam("pres_id") Long presId,
			@RequestParam("user_id") Long userId) {
		Presentation pres = new Presentation(presId);
		pres.setUser(new User(userId));
		this.presentationService.deletePresentation(pres);
		return "redirect:/administration/presentations";
	}

	// ----------------------------Edit------------------------------------
	@RequestMapping(method = RequestMethod.GET, value = "/edit")
	public String usersEdit(@RequestParam("pres_id") Long presId, Model model) {
		Presentation pres = null;
		if ((pres = this.presentationService.getPresentation(new Presentation(presId))) == null)
			return "redirect:/administration/presentations";
		model.addAttribute(AdminPresentationsController.EDIT_PRESFORM_ATTR_NAME,
				new EditPresentationForm(pres));
		model.addAttribute(AdminPresentationsController.PRESSTATUS_ATTR_NAME,
				this.presentationService.getPresentationStatuses());
		return AdminPresentationsController.EDIT_PRES_FILE_LOCATION;
	}

	@RequestMapping(method = RequestMethod.POST, value = "/edit")
	public String usersEdit(Model model, @Valid final EditPresentationForm form,
			final BindingResult result) {
		if (result.hasErrors()) {
			model.addAttribute(AdminPresentationsController.PRESSTATUS_ATTR_NAME,
					this.presentationService.getPresentationStatuses());
			return AdminPresentationsController.EDIT_PRES_FILE_LOCATION;
		}
		this.presentationService.updatePresentation(form.getPresentation());
		return "redirect:/administration/presentations/";
	}

	@RequestMapping("/deletegroup")
	public String usersGroupDelete(
			@ModelAttribute(AdminPresentationsController.PRESENTATIONS_ATTR_NAME) CheckedPresentationsList list) {
		List<Presentation> buff = list.getCheckedPresentations();
		if (!buff.isEmpty())
			this.presentationService.deletePresentations(buff);
		return "redirect:/administration/presentations/";
	}

	private PresentationService presentationService;

	@Autowired
	@Qualifier("presentationServiceImpl")
	public void setPresentationService(PresentationService presentationService) {
		this.presentationService = presentationService;
	}

}
